package com.mysoft.main.main.aspect;

import com.mysoft.common.annotation.DS;
import com.mysoftbase.base.holder.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author: JohnCoding
 * @Description: 切换数据源注解切面类
 * @date: 2020/4/30 10:24
 */
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {

    @Pointcut("@within(com.mysoft.common.annotation.DS) || @annotation(com.mysoft.common.annotation.DS)")
    public void pointCut(){

    }

    @Before("pointCut() && @annotation(DS)")
    public void doBefore(DS DS){
        DataSourceContextHolder.setDataSource(DS.value().getValue());
    }

    @After("pointCut()")
    public void doAfter(){
        DataSourceContextHolder.clear();
    }
}
